using System;
using System.Xml;

namespace Google.Apps.Provisioning.Actions
{
    /// <summary>
    /// Implements steps specific to a RetrieveUser API call.
    /// </summary>
    internal class RetrieveUserAction : BaseProvisioningAction
    {
        private string userName;
        private GoogleUser user;

        public GoogleUser GoogleUser
        {
            get { return user; }
        }

        public RetrieveUserAction( string userName ) : base(
            "Google.Apps.Provisioning.XmlTemplates.RetrieveUser.xml",
            "https://www.google.com/a/services/v1.0/Retrieve/Account" )
        {
            this.userName = userName;
        }

        public override void ProcessRequestXml( XmlDocument doc, XmlNamespaceManager ns )
        {
            doc.SelectSingleNode( "hs:rest/hs:queryData", ns ).InnerText = userName;
        }

        public override void ProcessResponseXml( XmlDocument doc, XmlNamespaceManager ns )
        {
            user = new GoogleUser(
                doc.SelectSingleNode( "hs:rest/hs:RetrievalSection/hs:userName", ns ).InnerText,
                string.Empty,
                doc.SelectSingleNode( "hs:rest/hs:RetrievalSection/hs:firstName", ns ).InnerText,
                doc.SelectSingleNode( "hs:rest/hs:RetrievalSection/hs:lastName", ns ).InnerText );
        }
    }
}
